/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.Component;
import org.aswing.JList;
import org.aswing.JScrollPane;
import org.aswing.ListCellFactory;
import org.aswing.ListModel;

import com.bourre.data.collections.IndexedArray;

import fever.data.validators.ValidationResources;
import fever.utils.StringUtil;

import fvaswing.components.forms.FvFormField;

/**
 * Combobox form field.
 * 
 * <p>Choices must be added to field before adding field into a form object.
 * 
 * <p>Example
 * {@code
 *   var combo : FvComboField = new FvComboField( "Your primary pccupation", true );
 *   combo.addChoice( "basket-ball" );
 *   combo.addChoice( "football" );
 *   combo.addChoice( "actionscript" ); *   combo.addChoice( "nothing...too tired" );
 * }
 *  
 * @author Romain Ecarnot
 */
class fvaswing.components.forms.FvListField extends FvFormField
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _list : IndexedArray;
	private var _minSelection : Number;
	
	private var _input : JList;
	private var _listModel : ListModel;
	private var _isModelEnabled : Boolean;
	private var _cellFactory : ListCellFactory;
	private var _selectionMode : Number;	private var _visibleRowsCount : Number;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 * 
	 * @param label Field's caption
	 * @param minSelection Minimum elements selection to be valid
	 * @param selectionMode ( optional ) Defines if user can select only one element or more<br />
	 * Available values are : 
	 * <ul>
	 *   <li>{@code JList.SINGLE_SELECTION}</li>
	 *   <li>{@code JList.MULTIPLE_SELECTION}</li>
	 * </ul>
	 * Default is calculated using {@code minSelection} argument
	 */
	public function FvListField( label : String, minSelection : Number, selectionMode : Number  )
	{
		super( label );
		
		_minSelection = ( minSelection != undefined ) ? minSelection : 0;
		_isValidationEnabled = _minSelection > 0;
		
		_isModelEnabled = false;
		_list = new IndexedArray();
		
		if( selectionMode != undefined ) _selectionMode = selectionMode;
		
		// force multiple selection is minSelection > 1
		if( _minSelection > 1 ) _selectionMode = JList.MULTIPLE_SELECTION;
		
		_visibleRowsCount = 5;
	}
	
	/**
     * Sets the preferred number of rows in the list that can be displayed.<br />
     * Default is 5.
     * 
     * <p>Must be defined before adding field into a form object.
     */ 
	public function setVisibleRowCount( n : Number ) : Void
	{
		if( n > 0 )	_visibleRowsCount = n;
	}
	
	/**
	 * Sets selected field value.
	 */
	public function setValue( values : Array ) : Void
	{
		super.setValue( values );
	}
	
	/**
	 * Returns selected value.
	 */
	public function getValue( ) : Array
	{
		return  _input.getSelectedValues();
	}
	
	/**
	 * Adds new choice to combobox.
	 * 
	 * <p>Choices must be added to field before adding field into a form object.
	 * 
	 * @param caption combobox data
	 */
	public function addChoice( o : Object ) : Void
	{
		_list.push( o );
	}
	
	/**
	 * Sets in one step the list content using passed-in {@code model} definition.
	 * 
	 * <p>When {@link #setModel()} is call, all added choices added with {@link #addChoice()} 
	 * are ignored.
	 * 
	 * <p>Model must be defined before adding field into a form object.
	 */
	public function setModel( model : ListModel ) : Void
	{
		_listModel = model;	
		_isModelEnabled = true;
	}
	
	/**
	 * Sets <strong>ListCellFactory</strong> to use for list cells rendering.
	 * 
	 * <p>Default use <strong>DefaultListCellFactory</strong> factory.
	 * 
	 * <p>Factory must be defined before adding field into a form object.
	 */
	private function setCellFactory( factory : ListCellFactory ) : Void
	{
		_cellFactory = factory;	
	}
	
	/**
	 * Clears field value.
	 */
	public function clear() : Void
	{
		_input.setSelectedValue( null );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Returns component dedicated to field value.
	 */
	private function _createValueComponent() : Component
	{
		if( _isModelEnabled ) _input = new JList( _listModel, _cellFactory );
		else _input = new JList( _list.getElements(), _cellFactory );
		
		_input.setSelectionMode( _selectionMode );
		_input.setVisibleRowCount( _visibleRowsCount );
		
		var scroll : JScrollPane = new JScrollPane( _input );
		return scroll;
	}
	
	private function _initWithData( values : Array ) : Void
	{
		_input.setSelectedValues( values );
	}
	
	/**
	 * Process validation
	 */
	private function _validateField() : Boolean
	{
		return _triggerResult( 
			_input.getSelectedIndices().length >= _minSelection, 
			StringUtil.substitute( ValidationResources.getInstance().range_minimum, _minSelection ) 
		);
	}
}